package com.lz.kunkun.messageservice.serviceImp;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.httpclient.HttpException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.lz.kunkun.messageservice.ServicesManager;
import com.lz.kunkun.messageservice.dao.MessageSendMapper;
import com.lz.kunkun.messageservice.model.MessageSend;
import com.lz.kunkun.messageservice.model.SysUser;
import com.lz.kunkun.messageservice.service.IMessageSendService;
import com.lz.kunkun.messageservice.util.WangJianHttpUtil;

/**
 * Hello world!
 *
 */
@Service("messageSendService")
@EnableScheduling
public class MessageSendServiceImpl implements IMessageSendService
{
	@Autowired
	MessageSendMapper messageSendMapper;

	@Override
	public int sendMes(MessageSend mes) {
		// TODO Auto-generated method stub
		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return messageSendMapper.sendMes(mes.getUserId(),mes.getPhoneNumber(),mes.getContent(),1,"待发送",sdf.format(new Date()),mes.getLevel());
	}

	@Override
	public List<MessageSend> allMes(Integer userId,Integer status,String keyword) {
		// TODO Auto-generated method stub
		return messageSendMapper.allMes(userId,status,keyword);
	}

	@Override
	public int delMes(Integer mesId, Integer userId) {
		// TODO Auto-generated method stub
		return messageSendMapper.delMes(mesId,userId);
	}	

	@Override
	public MessageSend getMes(Integer userId, Integer mesId) {
		// TODO Auto-generated method stub
		return messageSendMapper.getMes(userId,mesId);
	}

	@Override
	public int updMes(MessageSend ms) {
		// TODO Auto-generated method stub
		return messageSendMapper.updMes(ms.getMesId(),ms.getUserId(),ms.getPhoneNumber(),ms.getContent(),ms.getStatus(),ms.getWhy(),ms.getCreateTime(),ms.getLevel());
	}
	
	@Scheduled(cron="0/30 * * * * ?")
	public void postMessage() {
		List<MessageSend> list=messageSendMapper.getAllMes(1);
		if(list==null) {
			return;
		}
		for(MessageSend mes:list) {
			SysUser user=ServicesManager.instance.getISysUserService().getUserById(mes.getUserId());
			if(user.getAmount()==null||user.getAmount()<10) {
				messageSendMapper.updMesStatus(mes.getMesId(),0,"发送失败！账户余额不足！");
				continue;
			}
			if(mes.getStatus()!=1) {
				messageSendMapper.updMesStatus(mes.getMesId(),0,"发送失败！发送状态异常！");
				continue;
			}
			
			Integer status;
			try {
				status = WangJianHttpUtil.sendMes(mes.getPhoneNumber(), mes.getContent());
			} catch (IOException e) {
				e.printStackTrace();
				continue;
			}
			
			if(status>0) {
				messageSendMapper.updMesStatus(mes.getMesId(),2,"发送成功！扣费:"+status);
				ServicesManager.instance.getISysUserService().updateUserAmount(user.getUserId(),user.getAmount()-status);
			}else {
				messageSendMapper.updMesStatus(mes.getMesId(),0,WangJianHttpUtil.getReason(status));
			}
		}
	}

}
